import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: () => import('@/views/MyLogin.vue') },
  { path: '/reg', component: () => import('@/views/MyReg.vue') },
  {
    path: '/home',
    component: () => import('@/views/MyHome.vue'),
    children: [
      {
        path: '/home',
        component: () => import('@/views/MyChert.vue')
      },
      {
        path: '/user-info',
        component: () => import('@/views/user/UserInfo.vue')
      },
      {
        path: '/user-pwd',
        component: () => import('@/views/user/RePwd.vue')
      },
      { path: '/user-avatar', component: () => import('@/views/user/UserAratar.vue') },
      { path: '/art-cate', component: () => import('@/views/acticle/CateList.vue') },
      { path: '/art-list', component: () => import('@/views/acticle/ArtList.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/reg') {
    next()
  } else {
    if (store.state.user.token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
